<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Widget extends Gio_Core_Controller
{	
	public function installAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		$response = 'RESULT_NOT_OK';
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		
		$moduleId = $request->getPost('module_id');
		$widgetId = $request->getPost('widget_id');
		$title = $request->getPost('title');
		$description = $request->getPost('description');
		if (Modules_Core_Services_Widget::getById($widgetId, $moduleId) != null) {
			return;
		}
		$widget = array(
					'module_id' 	=> $moduleId,
					'widget_id' 	=> $widgetId,
					'title' 		=> $title,
					'description'	=> $description,
					'created_date'	=> date('Y-m-d H:i:s')
				);
		Modules_Core_Services_Widget::add($widget);
		$response = 'RESULT_OK';
		$this->getResponse()->setBody($response);
	}
	
	public function listAction()
	{
		$request = $this->getRequest();
		if ($request->isPost()) {
			$this->setNoRender();
			$this->disableLayout();
			
			$mod = $request->getPost('mod');
			$widgets = Modules_Core_Services_Widget::getWidgets($mod);
			$this->view->widgets = $widgets;
			
			$dbWidgets = Modules_Core_Services_Widget::dbWidgets($mod, true);
			$installedWidgets = array();
			if ($dbWidgets) {
				foreach ($dbWidgets as $widget) {
					$installedWidgets[] = $widget['module_id'] . '_' . $widget['widget_id'];
				}
			}
			$this->view->installedWidgets = $installedWidgets;
			
			$viewFile = ROOT_DIR . DS . 'templates' . DS . $this->view->APP_TEMPLATE
							. DS . 'modules' . DS . 'core' . DS . 'widget' . DS . 'widget.ajax.phtml';
			$response = $this->view->render($viewFile);
			$this->getResponse()->setBody($response);
			return;
		}
		
		$modules = Modules_Core_Services_Module::getModulesInstalled();
		/**
		 * Add module core to the top array 
		 */
		$modules = array_pad($modules, -count($modules) - 1, array('module_id' => 'core'));
		$this->view->modules = $modules;
	}
	
	public function uninstallAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		$response = 'RESULT_NOT_OK';
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		
		$moduleId = $request->getPost('module_id');
		$widgetId = $request->getPost('widget_id');
		if (null == Modules_Core_Services_Widget::getById($widgetId, $moduleId)) {
			return;
		}
		Modules_Core_Services_Widget::delete($widgetId, $moduleId);
		$response = 'RESULT_OK';
		$this->getResponse()->setBody($response);
	}
	
	public function ajaxAction()
	{
		$this->disableLayout()
			 ->setNoRender();
			 
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		
		$module 	  = ucfirst(strtolower($request->getPost('mod')));
		$name 		  = ucfirst(strtolower($request->getPost('name')));
		$cacheEnable  = $request->getPost('cache');
		$cacheEnable  = ($cacheEnable == 'true') ? true : false;
		$cacheTimeout = $request->getPost('timeout');
		$cacheTimeout = ($cacheTimeout > 0) ? $cacheTimeout : 3600;
		
		$params = array(
			'load'		=> 'ajax',
			'module'	=> $module,
			'widget'	=> $name,
		);
		$paramString = $request->getPost('params');
		if ($paramString) {
			$temp = explode('|', $paramString);
			foreach ($temp as $index => $param) {
				$data = explode('=', $param);
				if ($param && count($data) == 2) {
					$params[$data[0]] = $data[1];
				}
			}
		}
		
		$configs = Gio_Core_Config_Xml::getConfig('web');
		$params['lang'] = (!isset($params['lang']) || null == $params['lang'])
								? $configs->language : $params['lang'];
		$action = $request->getPost('act', 'show');
		
		$response = Modules_Core_Services_Widget::load($module, $name, $action, $params, $cacheEnable, $cacheTimeout);
		$json = new Services_JSON();
		$this->getResponse()->setBody($json->encodeUnsafe($response));
	}
}